package org.leiyang.common.util;

import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.security.Key;
import java.security.SecureRandom;

/**
 * 加解密工具
 */
public class CrpyUtil {
    private static final Logger log = LoggerFactory.getLogger(CrpyUtil.class);
    //初始化盐
    private static byte[] salt = null;
    private static Key key = null;
    private static final long randomInt = -971315596L;


    static {
        try {
            PBEKeySpec pbeKeySpec = new PBEKeySpec(Constants.encryptPwd.toCharArray());
            SecretKeyFactory factory = SecretKeyFactory.getInstance("PBEWITHMD5andDES");
            key = factory.generateSecret(pbeKeySpec);
            salt = new byte[] {
                    (byte)((randomInt>>56)&0xff),
                    (byte)((randomInt>>48)&0xff),
                    (byte)((randomInt>>40)&0xff),
                    (byte)((randomInt>>36)&0xff),
                    (byte)((randomInt>>24)&0xff),
                    (byte)((randomInt>>16)&0xff),
                    (byte)((randomInt>>8)&0xff),
                    (byte)(randomInt&0xff)
            };
        } catch (Exception e) {
            log.error("执行CrpyUtil初始化操作异常, ", e);
        }
    }

    /**
     * 加密
     */
    public static String encryptPBE(String content) {
        try {
            PBEParameterSpec pbeParameterSpec = new PBEParameterSpec(salt, 100);
            Cipher cipher = Cipher.getInstance("PBEWITHMD5andDES");
            cipher.init(Cipher.ENCRYPT_MODE, key, pbeParameterSpec);
            byte[] result = cipher.doFinal(content.getBytes());
            return Base64.encodeBase64String(result);
        } catch (Exception e) {
            log.error("执行encryptPBE出现异常, ", e);
        }
        return null;
    }

    /**
     * 解密
     */
    public static String decryptPBE(String encodData) {
        try {
            PBEParameterSpec pbeParameterSpec = new PBEParameterSpec(salt, 100);
            Cipher cipher = Cipher.getInstance("PBEWITHMD5andDES");
            cipher.init(Cipher.DECRYPT_MODE, key, pbeParameterSpec);
            byte[] result = cipher.doFinal(Base64.decodeBase64(encodData));
            return new String(result);
        } catch (Exception e) {
            log.error("执行decryptPBE出现异常, ", e);
        }
        return null;
    }

    public static void main(String[] args) throws UnsupportedEncodingException {
//        String c = "1@user:2";
//        String enc = encryptPBE(c);
//        System.out.println("加密: " + enc);
//        System.out.println("解密: " + decryptPBE("VtJnRpU9a3s3ZnYm2/Q0Vk2C7pqMV6pno0PZ/89ZGsw9imNPBe9zO7w3f1oai0zlFgiwJLw3e1A="));

//        String token = "6d3WLjqCv00V+ZR3nX3At/JpbB9hMNM0W+5yIsYKZfWmsBMDT8eE+NSYgYZoI8ousT550ZTnkGU=";
//        String encode = UriUtils.encode(token);
//        System.out.println(encode);
//        System.out.println(UriUtils.decode(encode, Charset.defaultCharset()));

        byte[] salt = new SecureRandom().generateSeed(8);
        System.out.println(ByteBuffer.wrap(salt).getInt());
    }

}